package com.tinygray;

/**
 * @Author: tinygray
 * @Description: 公众号:Madison龙少，关注我你会越来越优秀。
 * @className: test
 * @create: 2021-04-27 19:06
 */
public class test {
    public static void main(String[] args) {
        //多个线程操作同一个资源类  并发
        ticket ticket = new ticket();
        new Thread(()->{
            for (int i = 0; i < 50; i++) {
                ticket.sale();
            }
        },"A").start();
        new Thread(()->{
            for (int i = 0; i < 50; i++) {
                ticket.sale();
            }
        },"B").start();
        new Thread(()->{
            for (int i = 0; i < 50; i++) {
                ticket.sale();
            }
        },"C").start();
    }

}
class ticket {
    private int number = 100;
    //synchronized 队列 锁
    public synchronized void sale(){
        if (number > 0) {
            System.out.println(Thread.currentThread().getName() + "卖出了第" + (number--) + "票");
        }
    }
}
